<template>
  <view class="container">
	  <u-button @click="backtoadd" type="primary">确定</u-button>
    <u-checkbox-group v-model="selectedIds" >
      <u-cell-group>
        <u-cell
          v-for="item in list"
          :key="item.id"
          :title="item.title"
          @click="handleCellClick(item)"
        >
          <!-- 左侧复选框 -->
          <template #icon>
            <u-checkbox
              :name="item.id"
              @click.native.stop
			  @change="boxClick(item)"
              size="18"
            ></u-checkbox>
          </template>

          <!-- 右侧箭头 -->
          <template #right-icon>
            <u-icon name="arrow-right"></u-icon>
          </template>
        </u-cell>
      </u-cell-group>
    </u-checkbox-group>
  </view>
</template>

<script>
	import {
		baseUrl,
	} from '@/common/config.js'
	import {
		mapGetters
	} from 'vuex'
	import {
		getaddrbylevel,
		getaddrbypcode
	} from '@/common/api.js'
export default {
  data() {
    return {
      selectedIds: [], // 存储选中的ID
      list: [
        { id: 1, title: '列表项1', url: 'buildaddr?id=1' },
        { id: 2, title: '列表项2', url: 'buildaddr?id=2' },
        { id: 3, title: '列表项3', url: 'buildaddr?id=3' }
      ],
	  backto: 0
    }
  },
  onLoad(option) {
	  this.backto = parseInt(option.backto)
	  var nextback =  this.backto + 1
  	if(option.level){
		this.list = []
		getaddrbylevel(option.level).then(res=>{
			res.forEach(e => {
				this.list.push({
					id: e.AddrCode,
					title: e.Name,
					url: 'buildaddr?pcode=' + e.AddrCode + '&backto=' + nextback,
					fullname: e.FullName
				})
			})
		})
	}
	if(option.pcode){
		this.list = []
		getaddrbypcode(option.pcode).then(res=>{
			res.forEach(e => {
				this.list.push({
					id: e.AddrCode,
					title: e.Name,
					url: 'buildaddr?pcode=' + e.AddrCode + '&backto=' + nextback,
					fullname: e.FullName
				})
			})
		})
	}
	
  },
  onShow(){
	  this.selectId = getApp().globalData.buildAddrId
	  this.selectedIds =  getApp().globalData.buildAddrId
  },
  methods: {
    handleCellClick(item) {
      // 处理跳转逻辑
      uni.navigateTo({
        url: item.url
      })
    },
	// checkboxChange(e){
	// 	this.selectedIds = [...new Set([...getApp().globalData.buildAddrId, ...e])];
	// 	getApp().globalData.buildAddrId = this.selectedIds
	// 	this.selectId = this.selectedIds
	// },
	boxClick(item){
		const toggleArrayValue = (arr, value) => {
		  // 检查是否存在（使用严格相等判断）
		  const index = arr.findIndex(item => item === value);
		  // 存在则删除，不存在则添加
		  return index === -1 
			? [...arr, value] 
			: arr.filter((_, i) => i !== index);
		};
		getApp().globalData.buildAddrId = toggleArrayValue(getApp().globalData.buildAddrId,item.id)
		getApp().globalData.buildAddrName = toggleArrayValue(getApp().globalData.buildAddrName,item.fullname)
		this.selectedIds =  getApp().globalData.buildAddrId
	},
	backtoadd(){
		uni.navigateBack({
			delta:this.backto
		})
	}
  }
}
</script>

<style scoped>
.container {
  padding: 20rpx;
}

/* 调整复选框和标题的间距 */
 .u-cell__body {
  padding: 20rpx 0;
}

/* 自定义复选框样式 */
 .u-checkbox__icon-wrap {
  margin-right: 20rpx;
}
</style>